package com.cloudlibrary.mapper;

import com.cloudlibrary.domain.Book;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;


/**
* @author lenove
* @description 针对表【book】的数据库操作Mapper
* @createDate 2024-12-12 15:23:12
* @Entity com.cloudlibrary.domain.Book
*/

@Mapper
public interface BookMapper{
    @Select("SELECT * FROM book WHERE book_status !='3' order by book_uploadtime DESC ")
    @Results(id = "bookMap",value = {
            @Result(id = true, column = "book_id", property = "id"),
            @Result(column = "book_name", property = "name"),
            @Result(column = "book_isbn", property = "isbn"),
            @Result(column = "book_press", property = "press"),
            @Result(column = "book_author", property = "author"),
            @Result(column = "book_pagination", property = "pagination"),
            @Result(column = "book_price", property = "price"),
            @Result(column = "book_uploadtime", property = "uploadtime"),
            @Result(column = "book_status", property = "status"),
            @Result(column = "book_borrower", property = "borrower"),
            @Result(column = "book_borrowtime", property = "borrowTime"),
            @Result(column = "book_returntime", property = "returntime")
    })
    Page<Book> selectNewBooks();
    @Select("SELECT * FROM book where book_id = #{id}")
    @ResultMap("bookMap")
    Book findById(String id);



    @Select({"<script>" +
            "SELECT * FROM book " +
            "where book_status !='3'" +
            "<if test=\"name != null\"> AND  book_name  like  CONCAT('%',#{name},'%')</if>" +
            "<if test=\"press != null\"> AND book_press like  CONCAT('%', #{press},'%') </if>" +
            "<if test=\"author != null\"> AND book_author like  CONCAT('%', #{author},'%')</if>" +
            "order by book_borrowtime" +
            "</script>"
    })
    @ResultMap("bookMap")
    Page<Book> searchBooks(Book book);
    @Insert({"<script>",
            "INSERT INTO book ",
            "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">",
            "<if test=\"id != null\">",
            "book_id,",
            "</if>",
            "<if test=\"name != null\">",
            "book_name,",
            "</if>",
            "<if test=\"isbn != null\">",
            "book_isbn,",
            "</if>",
            "<if test=\"press != null\">",
            "book_press,",
            "</if>",
            "<if test=\"author != null\">",
            "book_author,",
            "</if>",
            "<if test=\"pagination != null\">",
            "book_pagination,",
            "</if>",
            "<if test=\"price != null\">",
            "book_price,",
            "</if>",
            "<if test=\"uploadtime != null\">",
            "book_uploadtime,",
            "</if>",
            "<if test=\"status != null\">",
            "book_status,",
            "</if>",
            "<if test=\"borrower != null\">",
            "book_borrower,",
            "</if>",
            "<if test=\"borrowTime != null\">",
            "book_borrowtime,",
            "</if>",
            "<if test=\"returntime != null\">",
            "book_returntime,",
            "</if>",
            "</trim>",
            "VALUES",
            "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">",
            "<if test=\"id != null\">",
            "#{id},",
            "</if>",
            "<if test=\"name != null\">",
            "#{name},",
            "</if>",
            "<if test=\"isbn != null\">",
            "#{isbn},",
            "</if>",
            "<if test=\"press != null\">",
            "#{press},",
            "</if>",
            "<if test=\"author != null\">",
            "#{author},",
            "</if>",
            "<if test=\"pagination != null\">",
            "#{pagination},",
            "</if>",
            "<if test=\"price != null\">",
            "#{price},",
            "</if>",
            "<if test=\"uploadtime != null\">",
            "#{uploadtime},",
            "</if>",
            "<if test=\"status != null\">",
            "#{status},",
            "</if>",
            "<if test=\"borrower != null\">",
            "#{borrower},",
            "</if>",
            "<if test=\"borrowTime != null\">",
            "#{borrowTime},",
            "</if>",
            "<if test=\"returntime != null\">",
            "#{returntime},",
            "</if>",
            "</trim>",
            "</script>"})
    Integer addBook(Book book);
    @Update({"<script>" ,
            "UPDATE book ",
            "<trim prefix=\"set\" suffixOverrides=\",\">",
            "<if test=\"name!= null\">",
            "book_name = #{name},",
            "</if>",
            "<if test=\"isbn!= null\">",
            "book_isbn = #{isbn},",
            "</if>",
            "<if test=\"press!= null\">",
            "book_press = #{press},",
            "</if>",
            "<if test=\"author!= null\">",
            "book_author = #{author},",
            "</if>",
            "<if test=\"pagination!= null\">",
            "book_pagination = #{pagination},",
            "</if>",
            "<if test=\"price!= null\">",
            "book_price = #{price},",
            "</if>",
            "<if test=\"uploadtime!= null\">",
            "book_uploadtime = #{uploadtime},",
            "</if>",
            "<if test=\"status!= null\">",
            "book_status = #{status},",
            "</if>",
            "<if test=\"borrower!= null\">",
            "book_borrower = #{borrower},",
            "</if>",
            "<if test=\"borrowTime!= null\">",
            "book_borrowtime = #{borrowTime},",
            "</if>",
            "<if test=\"returntime!= null\">",
            "book_returntime = #{returntime},",
            "</if>",
            "</trim>",
            "WHERE book_id = #{id}",
            "</script>"
    })
    @ResultMap("bookMap")
    Integer editBook(Book book);



    @Select(
            {"<script>" +
                    "SELECT * FROM book " +
                    "where book_borrower=#{borrower}" +
                    "AND book_status ='1'"+
                    "<if test=\"name != null\"> AND  book_name  like  CONCAT('%',#{name},'%')</if>" +
                    "<if test=\"press != null\"> AND book_press like  CONCAT('%', #{press},'%') </if>" +
                    "<if test=\"author != null\"> AND book_author like  CONCAT('%', #{author},'%')</if>" +
                    "or book_status ='2'"+
                    "<if test=\"name != null\"> AND  book_name  like  CONCAT('%',#{name},'%')</if>" +
                    "<if test=\"press != null\"> AND book_press like  CONCAT('%', #{press},'%') </if>" +
                    "<if test=\"author != null\"> AND book_author like  CONCAT('%', #{author},'%')</if>" +
                    "order by book_borrowtime" +
                    "</script>"})
    @ResultMap("bookMap")
//查询借阅但未归还的图书和待归还确认的图书
    Page<Book> selectBorrowed(Book book);

    @Select({"<script>"  +
            "SELECT * FROM book " +
            "where book_borrower=#{borrower}" +
            "AND book_status in('1','2')"+
            "<if test=\"name != null\"> AND  book_name  like  CONCAT('%',#{name},'%')</if>" +
            "<if test=\"press != null\"> AND book_press like  CONCAT('%', #{press},'%') </if>" +
            "<if test=\"author != null\"> AND book_author like  CONCAT('%', #{author},'%')</if>" +
            "order by book_borrowtime" +
            "</script>"})
    @ResultMap("bookMap")
//查询借阅但未归还的图书
    Page<Book> selectMyBorrowed(Book book);
}




